<template>
  <div>

    <div class="card" style="margin-bottom: 10px;">
      <el-input prefix-icon="Search" style="width: 300px; margin-right: 10px" placeholder="请输入名称查询"
                v-model="data.name"></el-input>
      <el-button type="primary" @click="load">查询</el-button>
      <el-button type="warning" style="margin: 0 10px" @click="reset">重置</el-button>
    </div>

    <div class="card" style="margin-bottom: 10px">
      <div style="margin-bottom: 10px">
        <el-button type="primary" @click="handleAdd">新增</el-button>
      </div>
      <el-table :data="data.tableData" style="width: 100%">
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" label="序号" width="70"/>
        <el-table-column prop="username" label="账号"/>
        <el-table-column prop="name" label="名称"/>
        <el-table-column prop="avatar" label="头像">
          <template v-slot="scope">
            <el-image style="width: 50px; height: 50px; border-radius: 50%" v-if="scope.row.avatar" :src="scope.row.avatar"
                      :preview-src-list="[scope.row.avatar]" preview-teleported></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="sex" label="性别"/>
        <el-table-column prop="phone" label="手机号"/>
        <el-table-column prop="account" label="余额"/>
        <el-table-column prop="role" label="角色"/>
        <el-table-column label="操作" width="260" fixed="right">
          <template #default="scope">
            <el-button type="primary" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="danger" @click="handledel(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="card" v-if="data.total">
      <el-pagination background layout="prev, pager, next" @current-change="load" :page-size="data.pageSize"
                     v-model:current-page="data.pageNum" :total="data.total"/>
    </div>

    <el-dialog v-model="data.formVisible" title="信息" width="40%" destroy-on-close>
      <el-form :model="data.form" label-width="100px" style="padding-right: 50px">
        <el-form-item label="账号" >
          <el-input v-model="data.form.username" autocomplete="off" :disabled="data.form.id"/>
        </el-form-item>
        <el-form-item label="名称">
          <el-input v-model="data.form.name" autocomplete="off"/>
        </el-form-item>
        <el-form-item label="性别">
          <el-select style="width: 100%" v-model="data.form.sex">
            <el-option value="男" label="男"></el-option>
            <el-option value="女" label="女"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="data.form.phone" autocomplete="off"/>
        </el-form-item>
        <el-form-item label="余额">
          <el-input v-model="data.form.account" autocomplete="off"/>
        </el-form-item>
        <el-form-item label="头像">
          <el-upload action="http://localhost:9441/files/upload" :on-success="handleFileUpload">
            <el-button type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="data.formVisible = false">取消</el-button>
        <el-button type="primary" @click="save">确定</el-button>
      </span>
      </template>
    </el-dialog>

  </div>
</template>

<script setup>
import {reactive} from "vue"
import request from "@/utils/request";
import {ElMessage} from "element-plus";

const data = reactive({
  tableData: [],
  total: 0,
  pageNum: 1,  // 当前的页码
  pageSize: 5,  // 每页的个数
  formVisible: false,
  form: {},
  name: ''
})

const load = () => {
  request.get('/user/selectPage', {
    params: {
      name: data.name,
      pageNum: data.pageNum,
      pageSize: data.pageSize,
    }
  }).then(res => {
    data.tableData = res.data?.list || []
    data.total = res.data.total
  })
}

load()
//重置
const reset = () => {
  data.name = null
  load()
}
//保存弹窗
const handleAdd = () => {
  data.form = {}
  data.formVisible = true
}
//保存或修改数据
const save = () => {
  request.request({
    method: data.form.id ? 'PUT' : 'POST',
    url: data.form.id ? '/user/update' : '/user/add',
    data: data.form
  }).then(res=>{
    if(res.code === '200'){
      ElMessage.success("操作成功")
      data.formVisible = false
      load() //重新加载表格数据

    }else{
      ElMessage.error(res.msg)
      // data.formVisible = false
    }
  })
}
//根据id删除数据
const handledel=(id)=>{
  ElMessageBox.confirm('删除后数据无法恢复，您确认删除吗？','确认删除',{ type: 'warning'}).then(res=>{
    request.delete('/user/delete/'+id).then(res=>{
      if(res.code === '200'){
        ElMessage.success("操作成功")
        load() //重新加载表格数据
      }else{
        ElMessage.error(res.msg)
      }
    })
  })
}

//编辑弹窗
const handleEdit =(row)=>{
  data.form = JSON.parse(JSON.stringify(row))
  data.formVisible = true
}

const handleFileUpload = (file) => {
  data.form.avatar = file.data
}
const open = (id) => {
  ElMessageBox.confirm(
      '确定要删除这条数据吗', '警告！！！',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true,
      }
  )
      .then(() => {
        handledel(id)
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '已取消',
        })
      })
}

</script>